package com.zhuxulin.nokia;

import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Vector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextField;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

public class OrderQuery implements CommandListener {
	private Display dis = null;
	private List orderList = null;
	public Command detailCMD = null;
	public Command backCMD = null;
	public Command deleteCMD = null;
	public Command getWINCMD = null;
	private TextField orderDateTF = null;
	private Displayable parent;
	private String loginName;
	private String password;

	OrderQuery(Display dis, Displayable parent, String loginName, String password) {
		this.dis = dis;
		this.parent = parent;
		this.loginName = loginName;
		this.password = password;
		init();
	}

	private void init() {
		if (this.dis != null) {
			getOrderList();
			this.backCMD = new Command("Back", 2, 1);
			this.getWINCMD = new Command("Get Win", 4, 1);

			this.deleteCMD = new Command("Delete", 8, 1);
			this.orderList.addCommand(this.backCMD);

			this.orderList.addCommand(List.SELECT_COMMAND);
			this.orderList.addCommand(this.getWINCMD);
			this.orderList.addCommand(this.deleteCMD);
			this.orderList.setCommandListener(this);
			this.dis.setCurrent(this.orderList);
		}
	}

	public void getOrderList() {
		try {
			RecordStore rs = RecordStore.openRecordStore("ordersDB", true);
			Vector orders = RMSHelper.orderList(rs);
			int count = orders.size();
			System.out.println("Data Size:" + orders.size());

			if (count > 0) {
				String[] orderIds = new String[count];
				for (int i = 0; i < orders.size(); i++) {
					Order o = (Order) orders.elementAt(i);
					orderIds[i] = o.getOrderId();
				}
				this.orderList = new List("Order", 3, orderIds, null);
			} else {
				String[] orderIds = { "Order is empty" };

				this.orderList = new List("Order", 3, orderIds, null);
			}
		} catch (RecordStoreException ex) {
			ex.printStackTrace();
		}
	}

	public void commandAction(Command cmnd, Displayable dsplbl) {
		if (cmnd == List.SELECT_COMMAND) {
			if ((this.orderList.size() != 0) && (!"Order is empty".equals(this.orderList.getString(this.orderList.getSelectedIndex())))) {
				String orderId = this.orderList.getString(this.orderList.getSelectedIndex());
				Order order = RMSHelper.getOrder(orderId);
				new OrderDetail(this.dis, this.orderList, this.loginName, this.password, order);
			}
		} else if (cmnd == this.deleteCMD) {
			String orderId = this.orderList.getString(this.orderList.getSelectedIndex());
			boolean flag = RMSHelper.deleteOrder(orderId);
			this.orderList.delete(this.orderList.getSelectedIndex());
			if (flag) {
				Alert alert = new Alert("Warm Tips", "Delete Order Success", null, AlertType.INFO);
				alert.setTimeout(5000);
				this.dis.setCurrent(alert);
			} else {
				Alert alert = new Alert("Warm Tips", "Delete Order Failure", null, AlertType.INFO);
				alert.setTimeout(5000);
				this.dis.setCurrent(alert);
			}
		} else if (cmnd == this.getWINCMD) {
			String orderId = this.orderList.getString(this.orderList.getSelectedIndex());
			orderId = orderId.substring(2, orderId.length());
			String url = Role.HOST + "getOrder?orderNumber=" + orderId + "&username=" + this.loginName + "&password=" + this.password;
			System.out.println("===" + url);
			HttpRequest hr = new HttpRequest();
			String result = hr.getRequest(url);
			try {
				new OrderWinDetail(this.dis, this.orderList, this.loginName, this.password, new String(result.getBytes("iso8859-1"), "utf-8"));
			} catch (UnsupportedEncodingException ex) {
				ex.printStackTrace();
			}
		} else if (cmnd == this.backCMD) {
			this.dis.setCurrent(this.parent);
		}
	}
}